package org.arrow.tap.pages;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry5.ajax.MultiZoneUpdate;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.arrow.db.dao.DaoException;
import org.arrow.db.dao.UserDAO;
import org.arrow.db.pojo.CodeUserTitle;
import org.arrow.tap.components.BasePage;

public class UserTitle extends BasePage{
	private static Log log = LogFactory.getLog(UserTitle.class);
	
	@Inject
	private UserDAO userDao;
	
    @Property
    @Persist
    private String activeStatus;
    
    @Property
    @Persist
    private String queryName;
    
	@Property
	private CodeUserTitle tempTitle;
	
	@InjectComponent
	private Zone viewZone;
	
	@InjectComponent
	private Zone editZone;
	
	@Property
	@Persist
	private boolean change;
	
	@Property
	@Persist
	private CodeUserTitle changeTitle;
	
	public List<CodeUserTitle> getTitleList() throws DaoException{
		CodeUserTitle ui=new CodeUserTitle();
    	if(queryName != null && queryName.length()>0)
    		ui.setTitleName(queryName);
    	if(activeStatus!=null){
    		if(activeStatus.equalsIgnoreCase("true"))
    			ui.setEnable(true);
    		else
    			ui.setEnable(false);
    	}
    	
    	return userDao.queryList(ui, 0, 0, null, true);
	}
	
    void setupRender(){
    }
    
    Object onSuccessFromQueryForm() throws DaoException{
    	change=false;
    	System.out.println("testing");
    	return new MultiZoneUpdate("viewZone", viewZone.getBody()).add("editZone", editZone.getBody());
    }
    
    Object onActionFromOffLine(Long id) throws DaoException{
    	change=false;
    	CodeUserTitle si=(CodeUserTitle)userDao.load(CodeUserTitle.class, id);
    	si.setEnable(false);
    	userDao.save(si);
    	return new MultiZoneUpdate("viewZone", viewZone.getBody()).add("editZone", editZone.getBody());
    }
    
    Object onActionFromOnLine(Long id) throws DaoException{
    	change=false;
    	CodeUserTitle si=(CodeUserTitle)userDao.load(CodeUserTitle.class, id);
    	si.setEnable(true);
    	userDao.save(si);
    	return new MultiZoneUpdate("viewZone", viewZone.getBody()).add("editZone", editZone.getBody());
    }
    
    Object onActionFromNew(){
    	change=true;
    	changeTitle=new CodeUserTitle();
    	
    	return editZone.getBody();
    }
    
    Object onActionFromCancel(){
    	change=false;
    	return editZone.getBody();
    }
    
    Object onActionFromEdit(Long id) throws DaoException{
    	changeTitle=(CodeUserTitle)userDao.load(CodeUserTitle.class, id);
    	if(changeTitle!=null)
    		change=true;
    	return editZone.getBody();
    }
    
    Object onSuccessFromSaveForm() throws DaoException{
    	if(changeTitle.getTitleId()==null){
    		changeTitle.setEnable(true);
    		changeTitle.setCreateDate(new Date());
    		changeTitle.setLastModifyDate(new Date());
    	}else{
    		changeTitle.setLastModifyDate(new Date());
    	}
    	userDao.save(changeTitle);
    	change=false;
    	return new MultiZoneUpdate("viewZone", viewZone.getBody()).add("editZone", editZone.getBody());
    }

    
}
